package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.AlbumItemVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {


    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Resource
    private CategoryFeignClient categoryFeignClient;

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    ExecutorService executorService;


    @Override
    public AlbumItemVo loadAlbumData(Long albumnId) {
        AlbumItemVo albumItemVo = new AlbumItemVo();
        CompletableFuture<AlbumInfo> future = CompletableFuture.supplyAsync(() -> {
            //根据专辑ID查询船机
            Result<AlbumInfo> albumInfoResult = this.albumInfoFeignClient.getAlbumInfo(albumnId);
            Assert.notNull(albumInfoResult, "专辑详情页，获取专辑远程调用失败");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑详情页对应专辑不存在");
            albumItemVo.setAlbumInfo(albumInfo);

            return albumInfo;
        }, executorService);

        CompletableFuture<Void> categoryFuture = future.thenAcceptAsync(albumInfo -> {
            //根据三级分类id查询一二三级分类
            Result<BaseCategoryView> baseCategoryViewResult = this.categoryFeignClient.getBaseCategoryViewBycategory3Id(albumInfo.getCategory3Id());
            Assert.notNull(baseCategoryViewResult, "专辑详情页，获取三级分类信息失败");
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            if (baseCategoryView != null) {
                albumItemVo.setBaseCategoryView(baseCategoryView);
            }
        }, executorService);


        CompletableFuture<Void> albumStatVoFuture = CompletableFuture.runAsync(() -> {
            //根据专辑id统计信息
            Result<AlbumStatVo> albumStatVoResult = this.albumInfoFeignClient.getAlbumStatByAlbumId(albumnId);
            Assert.notNull(albumStatVoResult, "专辑详情页，获取专辑信息分类失败");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            albumItemVo.setAlbumStatVo(albumStatVo);
        }, executorService);

        CompletableFuture<Void> userInfoByIdFuture = future.thenAcceptAsync(albumInfo -> {
            //根据主播id查询主播
            Result<UserInfo> userInfoById = this.userInfoFeignClient.getUserInfoById(albumInfo.getUserId());
            Assert.notNull(userInfoById, "专辑详情页，获取主播信息失败");
            albumItemVo.setAnnouncer(userInfoById.getData());
        }, executorService);
        CompletableFuture.allOf(categoryFuture,albumStatVoFuture,userInfoByIdFuture).join();

        return albumItemVo ;

    }
}
